package com.lagou.common;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponse<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    private ServerResponse(Integer code) {
        this.code = code;
    }

    private ServerResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ServerResponse(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    private ServerResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMsg(String successMsg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),successMsg);
    }

    public static <T> ServerResponse<T> createBySuccessData(T successData){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),successData);
    }

    public static <T> ServerResponse<T> createBySuccessMsgAndData(String successMsg,T successData){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),successMsg,successData);
    }

    public static <T> ServerResponse<T> createByErrorMsg(String errorMsg){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMsg);
    }
}
